import {ENV} from './env'

let BASE_URL = ENV[process.env.NODE_ENV].API_URL;
// if (process.env.NODE_ENV === 'development') {
//     // 开发环境配置接口地址
//     BASE_URL = "http://crm.mgm-iot.com:8667/dev/api/"
// 	// BASE_URL = " http://9hik9j.natappfree.cc/"
// }

// post请求封装
const contentType = 'application/x-www-form-urlencoded;charset=UTF-8';

function postRequest(option) {
	if (!option.headers) {
		option.headers = {};
		option.headers['Content-Type'] = contentType;
	}
	if(uni.getStorageSync("Admin-Token")){
		option.headers['Admin-Token'] = uni.getStorageSync("Admin-Token");
	}
	var promise = new Promise((resolve, reject) => {
        if(!option.isHideLoading){
            uni.showLoading({
            	title: '数据加载中',
            	mask: true
            })
		}
// console.log(option.url)
		uni.request({
			url: BASE_URL + option.url,
			data: option.data,
			method: "POST",
			header: option.headers,
			timeout: option.timeout ? option.timeout : 15000,
			success: function(res) {
				uni.hideLoading();
				if (res.data.code == 302) {
					let isShowModal = uni.getStorageSync('isShowModal') || 0;
					// console.log('isShowModal:', isShowModal);
					if (isShowModal) {
						return;
					}
					uni.setStorageSync('isShowModal', 1);
					uni.showModal({
						title: '请登录',
						content: '您已被登出，需要登录后才能继续',
						showCancel: false,
						success: (res) => {
							if (res.confirm) {
								/**
								 * 如果需要强制登录，使用reLaunch方式
								 */
								clearLocalStore();
							}
						}
					});
				} else if (res.data.code == 200) {
					resolve(res.data)
				} else {
					uni.showToast({
						icon: "none",
						title: res.data.msg ? res.data.msg : '网络错误'
					})
				}
			},
			error: function(e) {
				uni.hideLoading();
				uni.showToast({
					icon: "none",
					title: '数据请求失败！'
				})
				reject("网络出错");
			}
		});
	});

	return promise;
}

// get请求封装
function getRequest(option) {
	if (!option.headers) {
		option.headers = {};
		option.headers['Content-Type'] = contentType;
	}
	if(uni.getStorageSync("Admin-Token")){
		option.headers['Admin-Token'] = uni.getStorageSync("Admin-Token");
	}
	var promise = new Promise((resolve, reject) => {
		uni.request({
			url: BASE_URL + option.url,
			data: option.data,
			method: "GET",
			dataType: "json",
			header: option.headers,
			timeout: option.timeout ? option.timeout : 15000,
			success: function(res) {
				uni.hideLoading();
				if (res.data.code == 302) {
					let isShowModal = uni.getStorageSync('isShowModal') || 0;
					if (isShowModal) {
						return;
					}
					uni.setStorageSync('isShowModal', 1);
					uni.showModal({
						title: '请登录',
						content: '您已被登出，需要登录后才能继续',
						showCancel: false,
						success: (res) => {
							if (res.confirm) {
								/**
								 * 如果需要强制登录，使用reLaunch方式
								 */
								clearLocalStore();
							}
						}
					});
				} else if (res.data.code == 200) {
					resolve(res.data)
				} else {
					uni.showToast({
						icon: "none",
						title: res.data.msg ? res.data.msg : '网络错误'
					})
				}
			},
			error: function(e) {
				uni.hideLoading();
				uni.showToast({
					icon: "none",
					title: '数据请求失败！'
				})
				reject("网络出错");
			}
		});
	});
	return promise;
}

function clearLocalStore() {
	try {
		uni.removeStorageSync('Admin-Token');
		uni.removeStorageSync('authList');
		uni.removeStorageSync('psi');
		uni.removeStorageSync('refreshData');
		uni.removeStorageSync('userInfo');
		// 清除 本地存储 解决 登录后退出  杀死程序 再登录 重复跳转登录页
		// uni.removeStorageSync('loginUserInfo');

		uni.navigateTo({
			url: '/pages/login/login',
		});

		// 判断是否再内页token过期，在登录的时候用
		uni.setStorageSync('isBack', 1);
	} catch (e) {
		console.log(e)
	}
}
function getBASE_URL(){
	return BASE_URL;
}
module.exports = {
	post: postRequest,
	get: getRequest,
	clearLocalStore: clearLocalStore,
	getBASE_URL:getBASE_URL
};
